var express = require('express');
var router = express.Router();
var db = require('../mysql/db')

//获取挂号信息列表
router.get('/get', function (req, res, next) {
    let sql = 'SELECT * FROM register_info order by register_date desc, register_time asc';
    let str = '';
    db.query(sql, [], results => {
        res.json({
            errno: 0, message: results
        })
        //console.log(results)
    })
});

//根据挂号单号查询挂号信息
router.post('/select', function (req, res, next) {
    //console.log(req.body.registerId)
    let sql = "SELECT * FROM register_info WHERE register_id =" + req.body.registerId ;
    let str = '';
    db.query(sql, [], results => {
        res.json({
            errno: 0, message: results
        })
        //console.log(results)
    })
});

router.post('/delete', function (req, res, next) {
    const register = req.body
    //console.log(req.body)
    let sql = "DELETE FROM register_info WHERE register_id = '" + register.register_id + "'";
    db.query(sql, [], (err, results) => {
        res.json({
            errno: 0, message: results
        })
    })
});

let sql = "SELECT * FROM department_position"
var department_position_info
db.query(sql, [], results => {
    //console.log(sql)
    //console.log(results, "1111")
    department_position_info = results;
})
router.post('/set', function (req, res, next) {
    const register = req.body
    console.log(register)
    const register_department = register.register_department[0] + "-" + register.register_department[1]
    const doctor_name = register.register_department[2]
    let doctor_id, register_position
    //console.log(department_position_info)
    const state = 0;
    for (item in department_position_info) {
        // console.log(item, department_position_info[item])
        for (key in department_position_info[item]) {
            // console.log(key, department_position_info[item][key])
            if (department_position_info[item][key] === doctor_name) {
                //console.log(department_position_info[item])
                doctor_id = department_position_info[item].doctor_id
                register_position = department_position_info[item].position
            }
        }
    }
    // let sql = "SELECT * FROM department_position WHERE doctor_name = ' "+ doctor_name + " ' "
    // let sql = "SELECT * FROM department_position WHERE department_position.doctor_name = '游毛冰'"
    // let sql = "SELECT * FROM department_position WHERE doctor_name = ' " + doctor_name + " ' "
    // let str = '';
    // db.query(sql, [], results => {
    //     console.log(sql)
    //     console.log(results,"0000000")
    // }) 
    let sql = 'INSERT INTO register_info(register_id, self_name, self_id, doctor_name, doctor_id, register_date, register_time, register_department, register_position, register_status) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
    db.query(sql, [register.register_id, register.self_name, register.self_id, doctor_name, doctor_id, register.register_date, register.register_time, register_department, register_position, state], (err, results) => {
        res.json({
            errno: 0, message: results
        })
    })
});
// db.query(sql, function (err, result) {
//     if (err) {
//         console.log('[SELECT ERROR]：', err.message);
//     }
//     //console.log(result[0].register_date)
//     str = JSON.stringify(result);
//     //console.log(result);
// })

module.exports = router;
